package uk.org.landeg.euler.problems;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import uk.org.landeg.euler.PrimeGen;
import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;

public class Problem075 extends AbstractProblem {

	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem075());
		launcher.solve();
	}

	@Override
	public String getDescription() {
		return "Find the number of different lengths of wire that can form a right angle triangle in only one way.";
	}

	@Override
	public Integer getProblemNumber() {
		return 75;
	}

	@Override
	public Object solve() {
		Set<Integer> sols = new TreeSet<Integer>();
		long a,b,c,mult,lmult;
		long l;
		long count = 0;
		long msq,nsq;
		
		StringBuffer out;
		int max =0;
		final long lMax = 1500000;
		final long halfLMax = lMax/2;
		final long sqrtLMax = (long)Math.sqrt(lMax) + 1;
		byte sc[] = new byte[(int)lMax + 1];
		int dots = 0;
		for (long m = 2; m < lMax ; m++) {
			for (long n = 1; n < m ; n++) {
				if (!PrimeGen.isCoprime(n, m)) {
					continue;
				}
				l = 2 * m * (m+n);
				mult = 1; lmult = l;
				if (l > lMax) break;
				if (m == 613 && n == 611) {
					System.out.println("It hits the fan....");
				}
				do {
					try {
						sc[(int)lmult]++;
						lmult = (++mult) * (l);
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println(m  + "\t" + n);
						System.out.println(lmult + ", " + l + " : " + lMax);
						System.exit(0);
					}
				} while (lmult < lMax);
			}
			if (m > 0 && m%1000 == 0) {
				dots++;
				System.out.print(".");
				if (dots % 40 == 0) {
					dots = 0;
					System.out.println();
				}
			}
		}
		
		for (int scount : sc) {
			if (scount == 1) {
				count++;
			}
		}
		return  count;
	}

	Set<Long> primes = PrimeGen.getPrimes();
	Map<Long, Integer> solutions = new TreeMap<Long, Integer>();

	private class Triplet  {
		protected Integer a, b, c;
		protected Integer L;
		
		Triplet (Integer a, Integer b, Integer c) {
			this.a = a;
			this.b = b;
			this.c = c;
			this.L = a + b + c;
		}
	}
}
